﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Component;
public partial class BackEnd_CategoryList : System.Web.UI.UserControl
{
    public string _Session = "";
    bool _CSSX = true;
    public string _Url = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        _Url = Request.Url.ToString().Split('&')[0]+"&"+ Request.Url.ToString().Split('&')[1] + "&" + ConstString._Del + "=" + ConstString._Del + "&" + ConstString._idcat + "=";
        if (QueryHelper.GetQueryString(Request, ConstString._Del) == ConstString._Del)
        {
            int id = ConvertType.ToInt(QueryHelper.GetQueryString(Request, ConstString._idcat));
            Category cat = CategoryDB.GetObjectByid(id, true);
            if (CategoryDB.DeleteCat(cat) == 1)
                this.labMessage.Text = ConstString.xoaTC;
        }
        if (QueryHelper.GetQueryString(Request, ConstString._CSSX) == ConstString._ML)
            _CSSX = false;
        else
            _CSSX = true;
        DataBind(CategoryDB.GetAll_CSSX(true,_CSSX));
        Sessions.Cat = _Session = "<a href=\"" + Request.RawUrl + "\" >Danh mục</a>";
        grid.RowCancelingEdit += delegate {  Response.Redirect(Request.Url.ToString()); };
    }
    #region ----------------------------- Ham viet tay -------------------------------------
    void DataBind(object al)
    {
        if (al != null)
        {
            grid.DataKeyNames = new string[] {ColumCategory._Id};
            grid.DataSource = al;
            grid.DataBind();
        }
    }
    #endregion

    protected void grid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        object id = grid.DataKeys[e.RowIndex].Value;
        TextBox txtName = (TextBox)grid.Rows[e.RowIndex].FindControl("txtName");
        TextBox txtNameEL = (TextBox)grid.Rows[e.RowIndex].FindControl("txtNameEL");
        TextBox txtPriority = (TextBox)grid.Rows[e.RowIndex].FindControl("txtPriority");
        DropDownList ddlAct = (DropDownList)grid.Rows[e.RowIndex].FindControl("ddlAct");
        Category cat = CategoryDB.GetObjectByid(ConvertType.ToInt(id),true);
        cat.Name = txtName.Text;
        cat.NameEL = txtNameEL.Text;
        cat.Priority= ConvertType.ToInt(txtPriority.Text.Trim());
        cat.Active = ConvertType.ToBool(ddlAct.Text);
        cat.Save();
        Response.Redirect(Request.Url.ToString());
    }
    protected void grid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grid.EditIndex = e.NewEditIndex;
        DataBind(CategoryDB.GetAll_CSSX(true, _CSSX));
    }
    //protected void grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    //{
    //    try
    //    {
    //        object id = grid.DataKeys[e.RowIndex].Value;
    //        Category cat = CategoryDB.GetObjectByid(ConvertType.ToInt(id), true);
    //        if (CategoryDB.Delete(cat) == 1)
    //            this.labMessage.Text = ConstString.xoaTC;
    //    }
    //    catch { }
    //    DataBind(CategoryDB.GetAll_CSSX(true, _CSSX));
    //}
    protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grid.PageIndex = e.NewPageIndex;
        DataBind(CategoryDB.GetAll_CSSX(true, _CSSX));
    }
}
